<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字筛选器</title>
    <style>
        body {
            font-family: "微软雅黑", sans-serif;
            background-color: #f0f8ff;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #ffffff;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }

        input[type="text"] {
            width: 80%;
            padding: 10px;
            margin-bottom: 20px;
            border: 2px solid #007BFF;
            border-radius: 5px;
            font-size: 16px;
        }

        button {
            width: 85%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border: 1px solid #007BFF;
            border-radius: 5px;
            color: #333;
            font-size: 18px;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数字筛选器</h1>
        <input type="text" id="inputNumbers" placeholder="请输入多个数字，用空格分隔" />
        <button id="findButton">查找第一个偶数</button>
        <div id="result" class="result"></div>
    </div>

    <script>
        // coalesceFactory 函数实现
        const coalesceFactory = (valid) => (...args) => args.find(valid);

        // 判断数字是否为偶数
        const isEven = (num) => num % 2 === 0;

        // 获取按钮、输入框和结果展示区
        const inputNumbers = document.getElementById('inputNumbers');
        const findButton = document.getElementById('findButton');
        const resultDiv = document.getElementById('result');

        // 点击按钮时触发的函数
        findButton.addEventListener('click', () => {
            const inputText = inputNumbers.value.trim();
            const numberArray = inputText.split(' ').map(num => Number(num)).filter(num => !isNaN(num)); // 将输入的字符串转换为数字数组

            if (numberArray.length === 0) {
                resultDiv.textContent = '请输入有效的数字！';
                resultDiv.classList.add('error');
                return;
            }

            const findFirstEven = coalesceFactory(isEven);
            const firstEven = findFirstEven(...numberArray);

            if (firstEven !== undefined) {
                resultDiv.textContent = `第一个偶数是: ${firstEven}`;
                resultDiv.classList.remove('error');
            } else {
                resultDiv.textContent = '没有找到偶数！';
                resultDiv.classList.remove('error');
            }
        });
    </script>
</body>

</html>